/**
 * Author: Dmitry Ustimov
 * Date: 5/13/13
 * Time: 11:34 PM
 */

package com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.example;

public abstract class Handler<T> {

    private Handler<T> next;

    public void setNext(Handler<T> successor) {
        this.next = successor;
    }

    public abstract Boolean canHandle(T request);

    public Boolean handled;

    public abstract void doAction(T request);

    public void handle(T request) {
        if (canHandle(request)) {
            doAction(request);
            handled = true;
        }
        else {
            if (next != null) {
                next.handle(request);
                handled = next.handled;
            }
            else {
                handled = false;
            }
        }
    }

    public void handleChain(T request) {
        if (canHandle(request)) {
            doAction(request);
            handled = true;
        }
        next.handleChain(request);
    }
}
